/*
 * Copyright (c) 2012-2013, CS Bakery. All rights reserved.
 */

package testCase;

/*
 * 	@release Final Release
 *	@author  So Mo Kin
 *	@version 1.5.4
 *
 * 	@release Release 2
 *	@author  Lee Cheuk Wang
 *	@version 1.0.9
 */

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
import org.junit.Test;

import junit.framework.TestCase;

import csBakery.*;

public class IOTest extends TestCase {
	
	private IO io;
	private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
	private String separator;

	public void setUp()	{	
		io = new IO();
		
		//Set up for output stream testing
		System.setOut(new PrintStream(outContent));
		separator = System.getProperty("line.separator");

	}
	
	public void tearDown()	{
		System.setOut(null);
	}

	@Test
	//Testing main logic and main exception
	public void testMainProcessOptions(){
		inputChoice();
		String output = mainOutput();
		output += "Option Error" + separator;
		output += mainOutput();

		IO.processInput(io);
		
		assertEquals(output, outContent.toString());
	}

	@Test
	//UserOption Test Case 1: if inputFromUser can return the right option
	public void testUserInputCase1(){
		try{
			assertEquals(1,IO.inputFromUser("1",true));
		}catch (Exception e){};
	}
	@Test
	//UserOption Test Case 2: if inputFromUser will show the correct exception message "Option Error"
	public void testUserInputCase2(){
		try{
			IO.inputFromUser("-5",true);
		} catch (Exception e){
			assertEquals("Option Error",e.getMessage());}
	}
	@Test
	//UserOption Test Case 3: if inputFromUser will show the correct exception message "Option Error"
	public void testUserInputCase3(){
		try{
			IO.inputFromUser("16",true);
		} catch (Exception e){
			assertEquals("Option Error",e.getMessage());}
	}
	@Test
	//UserOption Test Case 4: if inputFromUser will show the correct exception message "Qty Error"
	public void testUserInputCase4(){
		try {
			IO.inputFromUser("-5",false);
		} catch (Exception e) {
			assertEquals("Qty Error", e.getMessage());
		}
	}
	
	@Test
	//Execute Command Test Case 1: Command execution succeed or not
	public void testExecuteCmdCase1(){
		assertFalse(io.executeCommand(3));
		assertFalse(io.executeCommand(4));
		assertFalse(io.executeCommand(5));
		assertFalse(io.executeCommand(6));
		assertFalse(io.executeCommand(7));
		assertFalse(io.executeCommand(8));
		
		//Unrecognized option
		assertFalse(io.executeCommand(-1));
	}
	@Test
	//Execute Command Test Case 2: Exit System
	public void testExecuteCmdCase2(){
		assertTrue(io.executeCommand(9));
	}

	@Test
	//Bread Update Test Case 1: is importing successful through IO execute Command
	public void testBreadUpdateCase1(){	
		inputStream(1);
		assertFalse(io.executeCommand(1));

	}
	@Test
	//Bread Update Test Case 2: will importing and selling do nothing for inputing 0
	public void testBreadUpdateCase2(){	
		inputStream(0);
		assertFalse(io.executeCommand(1));
		inputStream(0);
		assertFalse(io.executeCommand(2));
	}
	@Test
	//Bread Update Test Case 3: is selling successful through IO execute Command
	public void testBreadUpdateCase3(){	
		inputStream(1);
		assertFalse(io.executeCommand(2));
	}
	@Test
	//Bread Update Test Case 4: is selling rejected when existing current stock
	public void testBreadUpdateCase4(){	
		inputStream(-1);
		assertFalse(io.executeCommand(2));
	}

	//main output
	public String mainOutput(){
		String output = "Welcome to CS Bakery System, Please indicate your choices:" + separator;
		output += "1. Update bread stock (imported)" + separator;
		output += "2. Update bread stock (saled)" + separator;
		output += "3. View stock information" + separator;
		output += "4. Import stock from existing record" + separator;
		output += "5. Export stock record to file" + separator;
		output += "6. Show price and cost" + separator;
		output += "7. Generate day balance sheet" + separator;
		output += "8. Generate month balance sheet" + separator;
		output += "9. Exit the System" + separator;

		return output;
	}
	
	//Sample input
	public void inputStream(int sample){
		String input = "";;
		switch (sample){
			case 1: input = "5" + separator + "5" +separator; break;
			case 0: input = "0" + separator + "0" +separator; break;
			case -1: input = "1115" + separator + "0" + separator + "9" + separator; break;
		}
		io.setScanner(new Scanner(input));
	}
	//Sample option
	public void inputChoice(){
		String input = "10" + separator + "9" + separator;
		io.setScanner(new Scanner(input));
	}

}
